package vn.edu.saigontech.ncms.web.study;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.enumeration.Gender;
import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.OfferingMember;
import vn.edu.saigontech.ncms.model.info.Receipt;
import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.model.program.ProgramType;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.service.ReceiptService;
import vn.edu.saigontech.ncms.service.StudentService;
import vn.edu.saigontech.ncms.service.StudentTypeService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.util.ImportExport;
import vn.edu.saigontech.ncms.vo.CertificateReportItem;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class OfferingMemberAction extends BaseAction {
	
	private Long id;
	private Long offeringId;
	private Long receiptId;
	private String receiptNumber;
	
	private String firstName;
	private String lastName;
	private String nickName;
	private String gender;
	private String dateOfBirth;
	private String address;
	private String phoneNumber;
	private String email;
	private Long typeId;
	private String comment;
	private String studyNote;
	
	private String consultedDate;
	private Long programId;
	private String testDate;
	private Long consultantId;
	
	private Long targetOfferingId;
	private boolean sourceOfferingDeleted;

	public String info() {
		REQUEST().setAttribute("offeringMember", OfferingMemberService.INSTANCE.findById(this.id));
		return SUCCESS;
	}
	
	public String showEdit() {
		REQUEST().setAttribute("types", StudentTypeService.INSTANCE.findAll());
		REQUEST().setAttribute("offeringMember", OfferingMemberService.INSTANCE.findById(this.id));
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAllAreActive());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findByRole("Consulting"));
		return SUCCESS;
	}
	
	public String edit() {
		OfferingMember offeringMember = OfferingMemberService.INSTANCE.findById(this.id);
		offeringMember.setComment(this.studyNote);
		OfferingMemberService.INSTANCE.persist(offeringMember);
		Student student = StudentService.INSTANCE.findById(offeringMember.getReceipt().getStudent().getId());
		student.setFirstName(this.firstName);
		student.setLastName(this.lastName);
		student.setNickName(this.nickName);
		student.setGender(Gender.create(this.gender));
		student.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
		student.setAddress(this.address);
		student.setPhoneNumber(this.phoneNumber);
		student.setEmail(this.email);
		student.setType(StudentTypeService.INSTANCE.findById(this.typeId));
		student.setComment(this.comment);
		student.setConsultedDate(DateUtil.fromString(this.consultedDate, this.i18n("format.date")));
		student.setProgram(ProgramService.INSTANCE.findById(this.programId));
		student.setTestDate(DateUtil.fromString(this.testDate, this.i18n("format.date")));
		student.setConsultant(UserService.INSTANCE.findById(this.consultantId));
		StudentService.INSTANCE.persist(student);
		
		Offering offering = offeringMember.getReceipt().getOffering();
		
		this.offeringId = offering.getId();
		this.id = offeringMember.getId();
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.member.edit", student.getFullName(), offering.getName());
		return SUCCESS;
	}
	
	public String list() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		REQUEST().setAttribute("offeringMembers", OfferingMemberService.INSTANCE.findByOfferingId(this.offeringId));
		return SUCCESS;
	}
	
	public String showScoreReport() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("members", OfferingMemberService.INSTANCE.findPassedByOfferingId(this.offeringId));
		
		this.addLoggingDetail(BaseAction.REPORT, "logging.score.report", offering.getName());
		return SUCCESS;
	}
	
	public String showCertificate() throws Exception {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Collection<CertificateReportItem> reportItems = OfferingMemberService.INSTANCE.issueCertificate(this.offeringId);
		RESPONSE().setContentType("application/pdf");
		RESPONSE().setHeader("Content-disposition","attachment; filename=\""+ "Class" + this.offeringId +".pdf\"");
		boolean starter = offering.getLevel().getProgram().getProgramType().equals(ProgramType.CHILDREN);
		if(starter) {
			ImportExport.exportToPdfStream("certificate-Starter.jasper", null, reportItems, RESPONSE().getOutputStream());
		} else
			ImportExport.exportToPdfStream("certificate.jasper", null, reportItems, RESPONSE().getOutputStream());

		this.addLoggingDetail(BaseAction.REPORT, "logging.certificate.report", offering.getName());
		return SUCCESS;
	}
	
	public String showChangeOffering() {
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		return SUCCESS;
	}
	
	public String changeOffering() {
		Offering offering = OfferingService.INSTANCE.findById(offeringId);
		Offering targetOffering = OfferingService.INSTANCE.findById(targetOfferingId);
		String offeringName = offering.getName();
		
		OfferingMemberService.INSTANCE.changeOffering(this.offeringId, this.targetOfferingId, this.sourceOfferingDeleted);
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.offering.move", offeringName, targetOffering.getName());

		if (sourceOfferingDeleted) {
			if(OfferingService.INSTANCE.deleteById(offeringId)) {
			this.addLoggingDetail(BaseAction.DELETE, "logging.offering.delete", offeringName);
			}
		}
		return SUCCESS;
	}
	
	public String showChangeMemberOffering() {
		REQUEST().setAttribute("receipt", ReceiptService.INSTANCE.findById(this.receiptId));
		REQUEST().setAttribute("offering", OfferingService.INSTANCE.findById(this.offeringId));
		return SUCCESS;
	}
	
	public String changeMemberOffering() {
		Offering targetOffering = OfferingService.INSTANCE.findById(targetOfferingId);
		Receipt receipt = ReceiptService.INSTANCE.findById(receiptId);
		receipt.setOffering(targetOffering);
		
		String student = receipt.getStudent().getFullName();
		String original = receipt.getOffering().getName();
		String target = targetOffering.getName();
		
		ReceiptService.INSTANCE.persist(receipt);
		
		this.addLoggingDetail(BaseAction.EDIT, "logging.member.move", student, original, target);
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public Long getReceiptId() {
		return receiptId;
	}

	public void setReceiptId(Long receiptId) {
		this.receiptId = receiptId;
	}

	public String getReceiptNumber() {
		return receiptNumber;
	}

	public void setReceiptNumber(String receiptNumber) {
		this.receiptNumber = receiptNumber;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getTargetOfferingId() {
		return targetOfferingId;
	}

	public void setTargetOfferingId(Long targetOfferingId) {
		this.targetOfferingId = targetOfferingId;
	}

	public boolean isSourceOfferingDeleted() {
		return sourceOfferingDeleted;
	}

	public void setSourceOfferingDeleted(boolean sourceOfferingDeleted) {
		this.sourceOfferingDeleted = sourceOfferingDeleted;
	}

	public String getConsultedDate() {
		return consultedDate;
	}

	public void setConsultedDate(String consultedDate) {
		this.consultedDate = consultedDate;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public String getTestDate() {
		return testDate;
	}

	public void setTestDate(String testDate) {
		this.testDate = testDate;
	}

	public Long getConsultantId() {
		return consultantId;
	}

	public void setConsultantId(Long consultantId) {
		this.consultantId = consultantId;
	}

	public String getStudyNote() {
		return studyNote;
	}

	public void setStudyNote(String studyNote) {
		this.studyNote = studyNote;
	}

	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
}
